package net.hubt.itc.ui;


import net.hubt.itc.data.LookupCommand;
import net.hubt.itc.data.PrefsKeys;
import net.hubt.itc.data.RssItems;
import net.hubt.itc.data.TimeTableData;
import net.hubt.itc.data.TimeTableList;
import net.hubt.itc.data.TimeTableXMLParser;
import net.hubt.itc.io.net.HttpUtils;
import net.hubt.itc.ui.TimeTableActivity.GetTimeTableTask;
import net.hubt.itc.ui.utils.CTable;
import net.hubt.itc.util.LayoutUtils;
import net.hubt.itc.util.ListListener;
import net.hubt.itc.util.RssReader;
import net.hubt.itc.util.StringUtils;
import net.hubt.itc.util.XMLDocument;

import android.app.AlertDialog;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class MainUI extends BaseActivity{
	
	public static final String TAG=MainUI.class.getSimpleName();
	
	private HUBTApplication mApplicationContext;
	private boolean a;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_blue_theme);
        //addRss();
        executeGetTimeTableTask();
        
        addHandlers();               
        checkUserIdPrefs();               
    }
    @Override
    protected void onStart() {
    	super.onStart();    	
    	if(mApplicationContext!=null)
    		mApplicationContext.setPhoneNumber();    	    
    }
    private void executeGetTimeTableTask() {
		
		prepareParamsForTask();
		
		if (userClass != null){
			new GetTimeTableTask().execute(baseUrl + "/xml.php", "lop", userClass,
					"type", LookupCommand.CMD_TIMETABLE + "");
		}
	}
    
	class GetTimeTableTask extends AsyncTask<String, String, String> {

		ProgressDialog dlgProgress;
		@Override
		protected void onPreExecute() {		
			super.onPreExecute();
		}
		@Override
		protected String doInBackground(String... addresses) {
			try {
				//connect to network, get xml data
				String timetableXmlData = HttpUtils.urlContentPost(addresses);
				return timetableXmlData;
			} catch (Exception ex) {
				//network error
				return "100";
			}
		}
		@Override
		protected void onProgressUpdate(String... values) {
		
		}

		@Override
		protected void onPostExecute(String timetableXmlData) {
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainUI.this);
			if(preferences.contains("timetableXmlData"))
			{
				String str=preferences.getString("timetableXmlData","").toString();
				if(str.equals(timetableXmlData.toString()))
				{
					
				}
				else
				{
					preferences.edit().putString("timetableXmlData",timetableXmlData).commit();
					final Drawable drawableTop = getResources().getDrawable(R.drawable.checklist);
					Button btnClassLook=(Button)findViewById(R.id.btnClassLook);
					btnClassLook.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
				}
			}
			else
			{
				preferences.edit().putString("timetableXmlData",timetableXmlData).commit();
				
			}
			
		}
	}



    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    }
    
    /**
     * button event handlers
     */
        private void addHandlers(){
    	Button btnSettings=(Button)findViewById(R.id.btnSettings);
    	btnSettings.setOnClickListener(new OnClickListener() {			
			@Override 
			public void onClick(View v) {
				startActivity(new Intent(MainUI.this,BasicPrefsActivity.class));				
			}
		});
    	Button btnTranscript=(Button)findViewById(R.id.btnTranscript);
    	btnTranscript.setOnClickListener(new OnClickListener() {			
			@Override 
			public void onClick(View v) {
				startActivity(new Intent(MainUI.this,TranscriptActivity.class));				
			}
		});
    	
    	Button btnSchedule=(Button)findViewById(R.id.btnSchedule);
    	btnSchedule.setOnClickListener(new OnClickListener() {			
			@Override 
			public void onClick(View v) {
				startActivity(new Intent(MainUI.this,ScheduleActivity.class));				
			}
		});
    	/*
    	Button btnTimeTable=(Button)findViewById(R.id.btnTimeTable);
    	btnTimeTable.setOnClickListener(new OnClickListener() {			
			@Override 
			public void onClick(View v) {
				startActivity(new Intent(MainUI.this,TimeTableActivity.class));				
			}
		});
    	*/
    	Button btnClassLook=(Button)findViewById(R.id.btnClassLook);
    	btnClassLook.setOnClickListener(new OnClickListener() {			
			@Override 
			public void onClick(View v) {
				startActivity(new Intent(MainUI.this,ClassLookActivity.class));				
			}
		});
    	
    	Button btnHelp=(Button)findViewById(R.id.btnHelp);
    	btnHelp.setOnClickListener(new OnClickListener() {			
			@Override 
			public void onClick(View v) {
				startActivity(new Intent(MainUI.this,CreditsActivity.class));
						
			}
		});
    	
    	Button btnCardSwipe=(Button)findViewById(R.id.btnCardSwipe);
    	btnCardSwipe.setOnClickListener(new OnClickListener() {			
			@Override 
			public void onClick(View v) {
				startActivity(new Intent(MainUI.this,CardSwipeActivity.class));				
			}
		});
    	Button btnNews=(Button)findViewById(R.id.btnNews);
    	btnNews.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(MainUI.this,NewsListActivity.class));	
			}
		});
    }
    
//    /**
//     * init global preferences
//     */
//    public void initPrefs(){
//    	if(mApplicationContext==null){
//    		mApplicationContext=(HUBTApplication)getApplication();
//    	}
//    }
//    
    public void checkUserIdPrefs(){    	
    	if(mApplicationContext==null)
    		mApplicationContext=(HUBTApplication)getApplication();
    	if(StringUtils.isEmpty(mApplicationContext.getUserPrefs().getUserId())){
    		Toast.makeText(this, getString(R.string.ui_prefs_errors_id_not_found),Toast.LENGTH_LONG).show();
    		startActivity(new Intent(this, BasicPrefsActivity.class));	
    	}        	   
    	else {
    		Log.d(TAG,"checkPrefs found user_id");    		
    	}    	
    }
    public boolean hasUserIdPrefs(Application app){
    	String userId=mApplicationContext.getPrefsString(PrefsKeys.USER_ID, null);
    	if(StringUtils.isEmpty(userId)){
    		return false;
    	}
    	return true;
    }
}